/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.game.chinesechess;

import com.game.chinesechess.check.rangeSense;
import com.game.chinesechess.piece.chess;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.text.Font;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

public class Drawer extends Component {
    private Paint paint;
    private GameInfo gameInfo;
    private int b_x, b_y, e_x, e_y;
    private float windowWidth, windowHeight, gap, gapWidth, height;


    private Text text;

    public Drawer(Context context, boolean isLocal) {
        super(context);
        paint = new Paint();
        paint.setAntiAlias(true);
        setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                if (touchEvent.getAction() == TouchEvent.PRIMARY_POINT_DOWN) {
                    if (gameInfo != null && gameInfo.Winner == null&&gameInfo.turn==gameInfo.color) {
                        float touchX = touchEvent.getPointerPosition(0).getX();
                        float touchY = touchEvent.getPointerPosition(0).getY();
                        float boardMarginTop = (windowHeight - height) / 2;
                        switch (gameInfo.next_step) {
                            case SELECT:
                                b_x = Math.round((touchX - gap) / gapWidth);
                                b_y = (int) ((touchY - boardMarginTop - gap) / gapWidth);
                                if (gameInfo.color == chess.Color.RED) {
                                    if (rangeSense.canSelect(gameInfo.board, b_y, b_x, gameInfo.turn)) {
                                        drawChess(b_x, b_y);
                                        gameInfo.next_step = GameInfo.state.MOVE;
                                    }
                                }
                                else {
                                    if (rangeSense.canSelect(gameInfo.board, gameInfo.board.length - b_y - 1, gameInfo.board[0].length - b_x - 1, gameInfo.turn)) {
                                        drawChess(b_x, b_y);
                                        gameInfo.next_step = GameInfo.state.MOVE;
                                    }
                                }
                                break;
                            case MOVE:
                                e_x = Math.round((touchX - gap) / gapWidth);
                                e_y = (int) ((touchY - boardMarginTop - gap) / gapWidth);
                                if (gameInfo.color == chess.Color.BlACK) {
                                    b_x = gameInfo.board[0].length - b_x-1;
                                    b_y = gameInfo.board.length - b_y-1;
                                    e_x = gameInfo.board[0].length - e_x-1;
                                    e_y = gameInfo.board.length - e_y-1;
                                }
                                gameInfo.moveChess(b_y, b_x, e_y, e_x,true);
                                drawChess();
                                b_x = -1;
                                e_x = -1;
                                b_y = -1;
                                e_y = -1;
                                gameInfo.next_step = GameInfo.state.SELECT;
                                break;
                        }
                    }
                }
                return true;
            }
        });
    }

    public void setDrawParams(GameInfo gameInfo) {
        this.gameInfo = gameInfo;
        windowWidth = GameInfo.getWindowWidth();
        windowHeight = GameInfo.getWindowHeight();
        gap = windowWidth / (gameInfo.board[0].length + 1) / 2 + 10;
        gapWidth = (windowWidth - gap * 2) / (gameInfo.board[0].length - 1);
        height = gapWidth * (gameInfo.board.length - 1) + gap * 2;
        this.drawChess();
        invalidate();
    }

    public void setText(Text text) {
        this.text = text;
    }

    public void drawChess() {
        this.addDrawTask(new DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                if (gameInfo != null) {
                    chess[][] chesses = gameInfo.board;
                    if (gameInfo.color == com.game.chinesechess.piece.chess.Color.BlACK)
                        chesses = gameInfo.flip();
                    canvas.translate(0, (windowHeight - windowWidth) / 2);
                    paint.setStyle(Paint.Style.FILL_STYLE);
                    canvas.drawRect(0, 0, windowWidth, height, paint, new Color(Color.getIntColor("#e18c00")));

                    paint.setStyle(Paint.Style.STROKE_STYLE);
                    paint.setStrokeWidth(4);
                    paint.setColor(Color.BLACK);
                    for (int i = 0; i < gameInfo.board.length; i++)
                        canvas.drawLine(gap, gap + gapWidth * i, windowWidth - gap, gap + gapWidth * i, paint);
                    for (int i = 0; i < gameInfo.board[0].length; i++) {
                        canvas.drawLine(gap + gapWidth * i, gap, gap + gapWidth * i, gap + gapWidth * 4, paint);
                        canvas.drawLine(gap + gapWidth * i, gap + gapWidth * 5, gap + gapWidth * i, height - gap, paint);
                        if (i == 0 || i == gameInfo.board[0].length - 1) {
                            canvas.drawLine(gap + gapWidth * i, gap + gapWidth * 4, gap + gapWidth * i, gap + gapWidth * 5, paint);
                        }
                    }
                    canvas.drawLine(gap + gapWidth * 3, gap, gap + gapWidth * 5, gap + gapWidth * 2, paint);
                    canvas.drawLine(gap + gapWidth * 5, gap, gap + gapWidth * 3, gap + gapWidth * 2, paint);
                    canvas.drawLine(gap + gapWidth * 3, height - gap, gap + gapWidth * 5, height - gap - gapWidth * 2, paint);
                    canvas.drawLine(gap + gapWidth * 5, height - gap, gap + gapWidth * 3, height - gap - gapWidth * 2, paint);

                    paint.setTextSize(75);
                    paint.setFont(Font.SERIF);
                    canvas.drawText(paint, "楚河", gap + gapWidth, gap * 2 + gapWidth * 4);
                    canvas.drawText(paint, "汉界", gap + gapWidth * 5, gap * 2 + gapWidth * 4);

                    paint.setColor(Color.BLACK);
                    paint.setTextSize(70);
                    paint.setFont(Font.MONOSPACE);
                    for (int i = 0; i < gameInfo.board.length; i++) {
                        for (int j = 0; j < gameInfo.board[0].length; j++) {
                            if (chesses[i][j] != null) {
                                if (chesses[i][j].color == chess.Color.RED)
                                    paint.setColor(Color.RED);
                                canvas.drawText(paint, chesses[i][j].getName(), gap + gapWidth * j - 30, gap + gapWidth * i + 20);
                                paint.setColor(Color.BLACK);
                                canvas.drawCircle(gap + gapWidth * j, gap + gapWidth * i, gapWidth / 2 - 10, paint);
                            }
                        }
                    }
                }
            }
        });
    }

    public void drawChess(int x, int y) {
        this.addDrawTask(new DrawTask() {
            @Override
            public void onDraw(Component component, Canvas canvas) {
                if (gameInfo != null) {
                    chess[][] chesses=gameInfo.board;
                    if(gameInfo.color== com.game.chinesechess.piece.chess.Color.BlACK)
                        chesses=gameInfo.flip();
                    canvas.translate(0, (windowHeight - windowWidth) / 2);
                    paint.setStyle(Paint.Style.FILL_STYLE);
                    canvas.drawRect(0, 0, windowWidth, height, paint, new Color(Color.getIntColor("#e18c00")));

                    paint.setStyle(Paint.Style.STROKE_STYLE);
                    paint.setStrokeWidth(4);
                    paint.setColor(Color.BLACK);
                    for (int i = 0; i < gameInfo.board.length; i++)
                        canvas.drawLine(gap, gap + gapWidth * i, windowWidth - gap, gap + gapWidth * i, paint);
                    for (int i = 0; i < gameInfo.board[0].length; i++) {
                        canvas.drawLine(gap + gapWidth * i, gap, gap + gapWidth * i, gap + gapWidth * 4, paint);
                        canvas.drawLine(gap + gapWidth * i, gap + gapWidth * 5, gap + gapWidth * i, height - gap, paint);
                        if (i == 0 || i == gameInfo.board[0].length - 1) {
                            canvas.drawLine(gap + gapWidth * i, gap + gapWidth * 4, gap + gapWidth * i, gap + gapWidth * 5, paint);
                        }
                    }
                    canvas.drawLine(gap + gapWidth * 3, gap, gap + gapWidth * 5, gap + gapWidth * 2, paint);
                    canvas.drawLine(gap + gapWidth * 5, gap, gap + gapWidth * 3, gap + gapWidth * 2, paint);
                    canvas.drawLine(gap + gapWidth * 3, height - gap, gap + gapWidth * 5, height - gap - gapWidth * 2, paint);
                    canvas.drawLine(gap + gapWidth * 5, height - gap, gap + gapWidth * 3, height - gap - gapWidth * 2, paint);

                    paint.setTextSize(75);
                    paint.setFont(Font.SERIF);
                    canvas.drawText(paint, "楚河", gap + gapWidth, gap * 2 + gapWidth * 4);
                    canvas.drawText(paint, "汉界", gap + gapWidth * 5, gap * 2 + gapWidth * 4);

                    paint.setColor(Color.BLACK);
                    paint.setTextSize(70);
                    paint.setFont(Font.MONOSPACE);
                    for (int i = 0; i < gameInfo.board.length; i++) {
                        for (int j = 0; j < gameInfo.board[0].length; j++) {
                            if (chesses[i][j] != null) {
                                if (chesses[i][j].color == chess.Color.RED)
                                    paint.setColor(Color.RED);
                                canvas.drawText(paint, chesses[i][j].getName(), gap + gapWidth * j - 30, gap + gapWidth * i + 20);
                                paint.setColor(Color.BLACK);
                                canvas.drawCircle(gap + gapWidth * j, gap + gapWidth * i, gapWidth / 2 - 10, paint);
                            }
                        }
                    }
                    paint.setColor(new Color(Color.rgb(179, 235, 235)));
                    paint.setStyle(Paint.Style.FILL_STYLE);
                    canvas.drawCircle(gap + gapWidth * x, gap + gapWidth * y, gapWidth / 2 - 10, paint);
                    paint.setColor(Color.BLACK);
                    paint.setTextSize(70);
                    paint.setFont(Font.MONOSPACE);
                    if (chesses[y][x].color== chess.Color.RED)
                        paint.setColor(Color.RED);
                    canvas.drawText(paint, chesses[y][x].getName(), gap + gapWidth * x - 30, gap + gapWidth * y);
                }
            }
        });
    }
}